package com.dormitory.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.manage.mapper.ProfessionMapper;
import com.dormitory.manage.pojo.College;
import com.dormitory.manage.pojo.Profession;
import com.dormitory.manage.service.CollegeService;
import com.dormitory.manage.service.ProfessionService;
import com.dormitory.manage.vo.AddProfessionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description ProfessionServiceImpl
 * @date 2023/4/10 0010 14:24
 */
@Service
public class ProfessionServiceImpl extends ServiceImpl<ProfessionMapper, Profession> implements ProfessionService {
    @Resource
    private CollegeService collegeService;
    /**
    * @description 获取专业名称列表
    *
    * @returnType java.util.List<java.lang.String>
    * @author Administrator
    * @date  14:38
    */
    @Override
    public List<String> getProfessionNames() {
        List<Profession> professions = this.baseMapper.selectList(null);
        ArrayList<String> professionNames = new ArrayList<>();
        for (Profession p: professions) {
            professionNames.add(p.getProfessionName());
        }
        return professionNames;
    }

    @Override
    public List<Profession> getAll(Integer collegeId) {
        return this.baseMapper.selectList(new QueryWrapper<Profession>().eq("profession_college_id",collegeId));
    }

    @Override
    public boolean removeProfessionByCollegeId(Long id) {
        List<Profession> professions = this.baseMapper.selectList(new QueryWrapper<Profession>().eq("profession_college_id", id));
        for (Profession p: professions) {
            return this.baseMapper.deleteById(p) > 0;
        }
        return true;
    }

    @Override
    public boolean removeProfession(Long id) {
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    public Profession getProfessionInfo(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public boolean addProfession(AddProfessionVo addProfessionVo) {
        Profession profession = new Profession();
        Profession profession1 = this.baseMapper.selectOne(new QueryWrapper<Profession>().eq("profession_name", addProfessionVo.getProfessionName()));
        College college = this.collegeService.getCollege(addProfessionVo.getCollegeName());
        if (profession1 == null && college != null){
            profession.setProfessionName(addProfessionVo.getProfessionName());
            profession.setProfessionCollegeId(college.getCollegeId());
            profession.setProfessionClasses(addProfessionVo.getClasses());
            profession.setCreateTime(new Date());
            return this.baseMapper.insert(profession) > 0;
        }
        return false;
    }

    @Override
    public boolean updateProfession(Profession profession) {
        Profession profession1 = this.baseMapper.selectById(profession.getProfessionId());
        if (null != profession1){
            return this.baseMapper.updateById(profession) > 0;
        }
        return false;
    }
}
